<template>
  <!--
  难点:
    1. 处理上部导航最后一个显示不完全问题(利用空标签占位)
    2. 处理中部内容的存放及按需显示问题(利用子组件存储(父子传参))
-->
  <div class="home-container">
    <!-- 顶部搜索 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        size="small"
        type="info"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 上部导航 -->
    <!--
      animated  -> 开启切换动画
      swipeable -> 开启手势滑动
    -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in userChannels" :key="item.id">
        <article-list :channel="item"></article-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="showEditChannel = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 中部内容 -->
    <!-- 编辑导航栏弹出层 -->
    <van-popup
      v-model="showEditChannel"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <channel-edit :my-channels="userChannels" :active="active" @update-active="onUpdateActive"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
// 文章列表组件
import ArticleList from './components/ArticleList.vue'
// 弹出层编辑组件
import ChannelEdit from './components/ChannelEdit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storages.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      userChannels: [],
      // 编辑导航栏弹出层中控
      showEditChannel: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      const channels = getItem('TOUTIAO_CHANNELS')
      if (this.user || (!this.user && !channels)) {
        const { data } = await getUserChannels()
        this.userChannels = data.data.channels
      } else {
        this.userChannels = channels
      }
    },
    // isShow = false 为传入的第二个参数设置默认值(不传时默认为false)
    onUpdateActive (index, isShow = false) {
      this.active = index
      this.showEditChannel = isShow
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.van-nav-bar__title {
  max-width: unset;
}
.search-btn {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  border: none;
  font-size: 28px;
  .van-icon {
    font-size: 32px;
  }
}
/deep/ .channel-tabs {
  .van-tabs__wrap {
    position: fixed;
    z-index: 1;
    top: 92px;
    left: 0;
    right: 0;
    height: 82px;
  }
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    font-size: 30px;
    color: #777777;
  }
  .van-tab--active {
    color: #333333;
  }
  .van-tabs__nav--line {
    padding-bottom: 0;
  }
  .van-tabs__line {
    bottom: 8px;
    height: 6px;
    background-color: #3296fa;
    width: 31px !important;
  }
  .van-tabs__nav {
    overflow-x: auto;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    width: 66px;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 82px;
    background-color: #fff;
    background-color: rgba(255, 255, 255, 0.902);
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
}
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
}
</style>
